package Impl;

import Util.JDBCUtil;
import entity.Friend;
import sun.security.util.Length;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FriendDaoImpl implements FriendDao {
    Connection conn=null;
    PreparedStatement ps =null;
    ResultSet rs = null;
    UserDaoImpl userDao = new UserDaoImpl();

//    新增好友信息
    @Override
    public void save(Friend friend,String username) {
        String sql = "INSERT INTO "+username+" (fname,fsex,femail,fphone,fschool) VALUES(?,?,?,?,?)";
        Object[] param = {friend.getFname(),friend.getFsex(), friend.getFemail(),friend.getFphone(),friend.getFschool()};
        userDao.commonUp(sql, param);
        System.out.println("添加成功");
    }

//    修改好友信息
//    ToDo:电话号码必须添加
    @Override
    public void update(Friend friend, String name) {
        StringBuilder sql =new StringBuilder( "update "+name+" set fphone=?");
        ArrayList<Object> pList=new ArrayList<Object>();
        pList.add(friend.getFphone());
        if (friend.getFemail()!=null){
            sql.append(",femail=?");
            pList.add(friend.getFemail());
        }
        if (friend.getFsex()!=null){
            sql.append(",fsex=?");
            pList.add(friend.getFsex());
        }
        if(friend.getFname()!=null){
            sql.append(",fname=?");
            pList.add(friend.getFname());
        }
        if(friend.getFschool()!=null){
            sql.append(",fschool=?");
            pList.add(friend.getFschool());
        }
        sql.append(" where fid=?");
        pList.add(friend.getFid());
        Object[] param=pList.toArray();
        System.out.println(sql);
        userDao.commonUp(sql.toString(), param);
        System.out.println("更新成功");
    }



//    删除好友信息
    @Override
    public void delete(int fid, String name) {
        String sql = "delete from "+name+" where fid = ?";
        userDao.commonUp(sql,fid);
        System.out.println("删除成功");
    }

//    查询所有好友信息
    @Override
    public List<Friend> findAllfriend(String name) {
        List<Friend> friends = new ArrayList<>();
        String sql = "select * from "+name;
        System.out.println(sql);
        Friend f=null;
        try {
            conn = JDBCUtil.getConnection();
            ps =conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                f=new Friend();
                f.setFid(rs.getInt("fid"));
                f.setFname(rs.getString("fname"));
                f.setFphone(rs.getString("fphone"));
                f.setFsex(rs.getString("fsex"));
                f.setFemail(rs.getString("femail"));
                f.setFschool(rs.getString("fschool"));
                friends.add(f);
            }
            System.out.println("查询成功");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询失败");
        }finally {
            JDBCUtil.closeAll(conn,ps,rs);
        }
        return friends;
    }

//    分页查询好友信息
    @Override
    public List<Friend> findFriendsByPage(String name, int pageNow) {
        int pageCount = 5;
        conn = JDBCUtil.getConnection();
        List<Friend> friends = new ArrayList<>();
        String sql = "select * from "+name+" limit ?,?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1, (pageNow-1)*pageCount);
            ps.setObject(2, pageCount);
            rs = ps.executeQuery();
            while(rs.next()) {
                Friend friend = new Friend();
                friend.setFid(rs.getInt("fid"));
                friend.setFname(rs.getString("fname"));
                friend.setFsex(rs.getString("fsex"));
                friend.setFphone(rs.getString("fphone"));
                friend.setFemail(rs.getString("femail"));
                friend.setFschool(rs.getString("fschool"));
                friends.add(friend);
            }
            System.out.println("分页查询成功");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBCUtil.closeAll(conn,ps,rs);
        }
        return friends;
    }

//    根据好友姓名查询好友信息
    @Override
    public List<Friend> findFriendByName(String name, String fname) {
        Friend f = new Friend();
        List<Friend> friendList=new ArrayList<>();
        String sql = "select * from "+name+" where fname='"+fname+"'";
        try {
            conn = JDBCUtil.getConnection();
            ps =conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                f=new Friend();
                f.setFid(rs.getInt("fid"));
                f.setFname(rs.getString("fname"));
                f.setFphone(rs.getString("fphone"));
                f.setFsex(rs.getString("fsex"));
                f.setFemail(rs.getString("femail"));
                f.setFschool(rs.getString("fschool"));
                System.out.println("查询成功");
                friendList.add(f);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询失败");
        }finally {
            JDBCUtil.closeAll(conn,ps,rs);
        }
        return friendList;
    }

//    根据好友的id查询好友信息
    @Override
    public Friend findFriendById(String name, int id) {
        Friend friend = new Friend();
        String sql = "select * from "+name+" where fid="+id;
        try {
            conn=JDBCUtil.getConnection();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            if (rs.next()){
                friend.setFid(rs.getInt("fid"));
                friend.setFname(rs.getString("fname"));
                friend.setFphone(rs.getString("fphone"));
                friend.setFschool(rs.getString("fschool"));
                friend.setFsex(rs.getString("fsex"));
                friend.setFemail(rs.getString("femail"));
            }else{
                friend = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeAll(conn,ps,rs);
        }
        return friend;
    }


    public static void main(String[] args){
//        Friend f = new Friend("布莱比尔","男","412365781@126.com","45698712311","宁波大学");
//        FriendDaoImpl friendDao = new FriendDaoImpl();
//        List<Friend> friendByName = friendDao.findFriendByName("vivi", "黄不烦");
//        System.out.println(friendByName);
//        friendDao.save(f,"vivi");
//        friendDao.update(f,"vivi");
//        friendDao.delete(1,"vivi");
//        List<Friend> allfriend = friendDao.findAllfriend("vivi");
//        List<Friend> friendbypage = friendDao.findFriendsByPage("vivi",2);
//        System.out.println(friendbypage);
//        System.out.println(allfriend);
//        System.out.println("-------增加成功-------");
    }
}
